What is validate.js?
Validate.js is a lightweight JavaScript library for data validation. It provides a simple and flexible way to validate data structures, ensuring that they meet specified criteria. The library supports a wide range of validation rules and can be easily extended to include custom validations.
What are validate.js's main functionalities?
Basic Validation
This feature allows you to validate basic data structures against a set of predefined constraints. In this example, the 'name' field must be present and have a minimum length of 3 characters, and the 'email' field must be present and follow a valid email format.
const validate = require('validate.js');
const constraints = {
name: {
presence: true,
length: {
minimum: 3
}
},
email: {
presence: true,
email: true
}
};
const data = {
name: 'John Doe',
email: 'john.doe@example.com'
};
const validationResult = validate(data, constraints);
console.log(validationResult);
Custom Validation
This feature allows you to define custom validation rules. In this example, a custom validator is created to check if the 'customField' matches the expected value. If it doesn't, a custom error message is returned.
const validate = require('validate.js');
validate.validators.customValidator = function(value, options, key, attributes) {
if (value !== options.expectedValue) {
return options.message || `${key} is not valid`;
}
};
const constraints = {
customField: {
customValidator: {
expectedValue: 'expectedValue',
message: 'Custom validation failed'
}
}
};
const data = {
customField: 'wrongValue'
};
const validationResult = validate(data, constraints);
console.log(validationResult);
Asynchronous Validation
This feature supports asynchronous validation, which is useful for scenarios where validation depends on external data or processes. In this example, an asynchronous validator checks if the 'asyncField' matches the expected value after a delay.
const validate = require('validate.js');
validate.validators.asyncValidator = function(value, options, key, attributes) {
return new validate.Promise(function(resolve, reject) {
setTimeout(function() {
if (value !== options.expectedValue) {
resolve(options.message || `${key} is not valid`);
} else {
resolve();
}
}, 1000);
});
};
const constraints = {
asyncField: {
asyncValidator: {
expectedValue: 'expectedValue',
message: 'Asynchronous validation failed'
}
}
};
const data = {
asyncField: 'wrongValue'
};
validate.async(data, constraints).then(function(validationResult) {
console.log(validationResult);
});
Other packages similar to validate.js
joi
Joi is a powerful schema description language and data validator for JavaScript. It allows you to create blueprints or schemas for JavaScript objects to ensure validation of key information. Compared to Validate.js, Joi offers a more extensive and expressive API for defining complex validation rules and is widely used in Node.js applications.
yup
Yup is a JavaScript schema builder for value parsing and validation. It is similar to Joi but is often preferred for its simplicity and ease of use, especially in React applications. Yup provides a fluent API for object schema validation and integrates well with form libraries like Formik.
validator
Validator is a library of string validators and sanitizers. It is less comprehensive than Validate.js, focusing primarily on string validation and sanitization. It is lightweight and easy to use for common validation tasks such as checking email formats, URLs, and other string-based validations.
validate.js
Validate.js provides a declarative way of validating javascript objects.
For documentation please see validatejs.org/.
For issues and feature requests visit the issue tracker.
Building validate.js
Requirements
Build steps
git clone git@github.com:ansman/validate.js.git
cd validate.js
npm install
grunt build
This will build validate.min.js, validate.min.map and the docs folder.
Continuous testing
You can run continuous testing that runs the tests on file changes by running
grunt watch
or simply grunt
.
If you want to just run the tests once you can do that by running grunt test
.
Build status
Contributing
Before opening a pull request please make sure your changes follow the
contribution guidelines.
Users of validate.js
If your site, library or application uses validate.js and would like to be shown
here please feel free to email info@validatejs.org
with the name and optionally a URL to the project and it will be added here.